package com.ebt.m.cloud;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.customer.util.DisplayUtil;
import com.ebt.m.widget.BaseActivity;
import com.ebt.m.widget.SlidingTabLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author Damon.Han
 */
public class ActCloudCache extends BaseActivity {
    @BindView(R.id.btn_cancel)
    FrameLayout btnCancel;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.btn_edit)
    TextView btnEdit;
    @BindView(R.id.bottom_view)
    LinearLayout bottom_view;
    @BindView(R.id.tv_select_all)
    TextView tv_select_all;
    @BindView(R.id.tv_delete)
    TextView tv_delete;
    @BindView(R.id.sliding_tab_layout)
    SlidingTabLayout mSlidingTabLayout;
    @BindView(R.id.vp_content)
    ViewPager mVpContent;
    private String[] titles;
    private Context mContext;
    private boolean isEdit;
    private DownloadedFragment downloadedFragment;
    private DownloadingFragment downloadingFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.act_cache);
        ButterKnife.bind(this);
    }

    @Override
    protected void init() {
        super.init();
        List<String> titles = new ArrayList<>();
        titles.add("已缓存");
        titles.add("缓存中");
        mVpContent.setAdapter(new MPagerAdapter(getSupportFragmentManager(), mContext, titles));
        mSlidingTabLayout.setDistributeEvenly(true);

        mSlidingTabLayout.setDistributeEvenlyWhenWidthNotFull(true, DisplayUtil.getScreenWidth(mContext));
        mSlidingTabLayout.setViewPager(mVpContent);
        mVpContent.setOffscreenPageLimit(2);
        mVpContent.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public class MPagerAdapter extends FragmentStatePagerAdapter {

        private List<String> list;

        public MPagerAdapter(FragmentManager fm, Context ctx, List<String> list) {
            super(fm);
            this.list = list;
        }

        @Override
        public Fragment getItem(int position) {

            if (position == 0) {
                downloadedFragment = DownloadedFragment.newInstance();
                return downloadedFragment;
            } else {
                downloadingFragment = DownloadingFragment.newInstance();
                return downloadingFragment;
            }

        }

        @Override
        public CharSequence getPageTitle(int position) {
            String title = list.get(position);
            return title;
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }

    /**
     * 修改标题
     * @param position
     * @param title
     */
    public void changePageTitle(int position, String title) {
        mSlidingTabLayout.getTextViewByPosition(position).setText(title);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @OnClick({R.id.btn_cancel, R.id.btn_edit, R.id.tv_select_all, R.id.tv_delete, R.id.sliding_tab_layout, R.id.vp_content})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_cancel:
                finish();
                break;
            //全选
            case R.id.tv_select_all:
                if (mVpContent.getCurrentItem() == 0) {
                    downloadedFragment.selectAll();
                } else {
                    downloadingFragment.selectAll();
                }
                break;
            case R.id.tv_delete:
                if (mVpContent.getCurrentItem() == 0) {
                    downloadedFragment.delete();
                } else {
                    downloadingFragment.delete();
                }
                break;
            case R.id.btn_edit:
                isEdit = !isEdit;
                if (isEdit) {
                    bottom_view.setVisibility(View.VISIBLE);
                    btnEdit.setText("取消");
                } else {
                    bottom_view.setVisibility(View.GONE);
                    btnEdit.setText("编辑");
                }
                if (mVpContent.getCurrentItem() == 0) {
                    downloadedFragment.toggleEdit(isEdit);
                } else {
                    downloadingFragment.toggleEdit(isEdit);
                }
                break;
        }
    }
}
